package com.leyou.item.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.service.IBrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 处理请求和响应的方法
 *
 * @version V1.0
 * @package com.leyou.item.service.impl
 * @description:
 * @author: pc
 * @date: 2019/6/25 15:07
 */

@Service("brandServiceImpl")
public class BrandServiceImpl implements IBrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Override
    public PageResult<BrandDTO> queryBrandByPage(String key, Integer page, Integer rows, String sortBy, Boolean desc) {
        //自动分页助手
        PageHelper.startPage(page, rows);
        //过滤查询条件
        Example example = new Example(Brand.class);
        //判断是否按搜索字查询
        if (StringUtils.isNotBlank(key)) {
            example.createCriteria().orLike("name","%"+key+"%").orEqualTo("letter",key.toUpperCase());
        }
        //判断是否排序
        if (StringUtils.isNotBlank(sortBy)) {
            String orderByClause = sortBy +  (desc ? " DESC" : " ASC");
            example.setOrderByClause(orderByClause);
        }
        //根据查询条件查询
        List<Brand> brands = brandMapper.selectByExample(example);
        //查询条件为空，返回错误信息
        if (CollectionUtils.isEmpty(brands)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        //解析分页信息
        PageInfo<Brand> info = new PageInfo<>(brands);
        //数据转换
        List<BrandDTO> result = BeanHelper.copyWithCollection(brands, BrandDTO.class);
        //返回结果
        return new PageResult<>(info.getTotal(), result);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertBrand(BrandDTO brandDTO, List<Long> cids) {
        //数据转换
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
        //清空id
        brand.setId(null);
        //执行添加
        int count = brandMapper.insertSelective(brand);
        //失败报错
        if (count != 1) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }

        //添加中间表
        Long bid = brand.getId();

        count = brandMapper.insertCategoryBrand(bid,cids);

        if (count != cids.size()) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    @Override
    public BrandDTO queryById(Long brandId) {
        Brand brand = brandMapper.selectByPrimaryKey(brandId);
        if (brand == null) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyProperties(brand,BrandDTO.class);
    }

    @Override
    public List<BrandDTO> queryBrandByCategoryId(Long cid) {

        List<Long> list = brandMapper.selectBrandIdFromCategoryParam(cid);

        List<Brand> brands = brandMapper.selectByIdList(list);

        return BeanHelper.copyWithCollection(brands,BrandDTO.class);

        //多表查询方式
       /* List<Brand> list = brandMapper.selectBrandByCategoryId(cid);

        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }

        return BeanHelper.copyWithCollection(list, BrandDTO.class);*/
    }

    @Override
    public List<BrandDTO> queryBrandByIds(List<Long> ids) {
        List<Brand> list = brandMapper.selectByIdList(ids);
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, BrandDTO.class);
    }
}
